package com.webservice.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.webservice.dao.IAllotOutBillBackDao;
import com.webservice.vo.AllotOutBillDetailInfo;
import com.webservice.vo.AllotOutBillImeiInfo;

@Repository("AllotOutBillBackDao")
public class AllotOutBillBackDaoImpl implements IAllotOutBillBackDao {

	@Autowired
	@Qualifier("datatemplate")
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public int updateOutBill(String outcode) {
		// TODO Auto-generated method stub
		System.out.println("更新出库单状态为已出库：" + "UPDATE BI_OUTSTORE SET ALLOTSTATE='3' WHERE OUTBILL='"+outcode+"' AND DR=0");
		int i=jdbcTemplate.update("UPDATE BI_OUTSTORE SET ALLOTSTATE='3' WHERE OUTBILL='"+outcode+"' AND DR=0");
		return i;
	}
	
	@Override
	public int updateAllotState(String allotcode) {
		// TODO Auto-generated method stub
		System.out.println("调拨单状态为在途：" + "UPDATE BI_ALLOT SET ALLOTSTATUS='7' WHERE ALLOTCODE='"+allotcode+"' AND DR=0");
		int i=jdbcTemplate.update("UPDATE BI_ALLOT SET ALLOTSTATUS='7' WHERE ALLOTCODE='"+allotcode+"' AND DR=0");
		return i;
	}

	@Override
	public int[] batchInsertOutBillDetail(List<AllotOutBillDetailInfo> details) {
		// TODO Auto-generated method stub
//		String sql="INSERT INTO BI_OUTBILLDETAIL(PK_OUTBILLDETAIL,BILLCODE,QUANTITY,PRODUCTID,DR,TS,ACTUALNUM,SUPPLYID) VALUES (?,?,?,?,?,?,?,?)";
		String sql="UPDATE  BI_OUTBILLDETAIL SET TS=? , ACTUALNUM=? WHERE PK_OUTBILLDETAIL=?  AND DR=0";
		List<Object[]> batchArgs=new ArrayList<Object[]>();
		for(AllotOutBillDetailInfo detail:details){
			Object[] args=new Object[3];
			args[0]=detail.getTs();
			args[1]=detail.getActualnum();
			args[2]=detail.getPk_outbilldetail();
			batchArgs.add(args);
		}
		int[] count=jdbcTemplate.batchUpdate(sql, batchArgs);
		return count;
	}

	@Override
	public int[] batchInsertOutBillImei(List<AllotOutBillImeiInfo> details) {
		// TODO Auto-generated method stub
		String sql="INSERT INTO BI_OUTIMEI(PK_OUTIMEI,PK_OUTBILLDETAIL,IMEI,DR,TS) VALUES (?,?,?,?,?)";
		List<Object[]> batchArgs=new ArrayList<Object[]>();
		for(AllotOutBillImeiInfo detail:details){
			Object[] args=new Object[5];
			args[0]=detail.getPk_outimei();
			args[1]=detail.getPk_outbilldetail();
			args[2]=detail.getImei();
			args[3]=detail.getDr();
			args[4]=detail.getTs();
			batchArgs.add(args);
		}
		int[] count=jdbcTemplate.batchUpdate(sql, batchArgs);
		return count;
	}


	@Override
	public int getQuantity(String allotcode, String productcode, String supplyid) {
		// TODO Auto-generated method stub
		int quantity=0;
		String sql="SELECT NVL(QUANTITY,0) AS QUANTITY FROM BI_ALLOTDETAIL WHERE ALLOTCODE=? AND SUPPLYID=? AND PRODUCTID=?  AND DR=0 AND ROWNUM=1";
		Object[] parms={allotcode,supplyid,productcode};
		List<String> li =  (List<String>)jdbcTemplate.query(sql, parms, new RowMapper<String>(){
			public String mapRow(ResultSet rs,int i) throws SQLException{
				String vo=rs.getString(1);
				return vo;
			}
		});
		if(li!=null&&li.size()>0){
			String q=li.get(0);
			if("".equals(q)){
				quantity=0;
			}else{
				quantity=Integer.parseInt(q);
			}
		}else{
			quantity=0;
		}
//		quantity=this.jdbcTemplate.queryForInt(sql,parms);
		return quantity;
	}
	
	@Override
	public String getOutBillDetailPk(String billcode, String productcode, String supplyid) {
		// TODO Auto-generated method stub
		String pk="";
		String sql="SELECT PK_OUTBILLDETAIL FROM BI_OUTBILLDETAIL WHERE BILLCODE=? AND SUPPLYID=? AND PRODUCTID=?  AND DR=0 AND ROWNUM=1";
		Object[] parms={billcode,supplyid,productcode};
		List<String> li =  (List<String>)jdbcTemplate.query(sql, parms, new RowMapper<String>(){
			public String mapRow(ResultSet rs,int i) throws SQLException{
				String vo=rs.getString(1);
				return vo;
			}
		});
		if(li!=null&&li.size()>0){
			pk=li.get(0);
		}
//		quantity=this.jdbcTemplate.queryForInt(sql,parms);
		return pk;
	}
	
	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}
	
	public String getOutStoreState(String outCode) {
		String sql="SELECT T.ALLOTSTATE FROM BI_OUTSTORE T WHERE T.OUTBILL=?  AND DR=0";
		List<String> list=this.jdbcTemplate.query(sql, new Object[]{outCode}, new RowMapper<String>() {
			@Override
			public String mapRow(ResultSet rs, int i) throws SQLException {
				return rs.getString(1);
			}
		});
		
		if(list!=null&&list.size()>0){
			return list.get(0);
		}
		return "";
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

}
